#include "Rotation90BaseParent.h"

namespace irr
{
	namespace scene
	{
		Rotation90BaseParent::Rotation90BaseParent(u32 time, const core::vector3df& rotation)
			: Rotation(rotation), StartTime(time), HasFinished(false)
		{
#ifdef _DEBUG
			setDebugName("Rotation90BaseParent");
#endif
		}

		void Rotation90BaseParent::animateNode(ISceneNode* node, u32 timeMs)
		{
			if (node)
			{
				const u32 diffTime = timeMs - StartTime;

				if (diffTime != 0)
				{
					core::vector3df rot = node->getRotation() + Rotation*(diffTime*0.1f);
					rot.X = core::clamp(rot.X, -90.f, 90.f);
					rot.Y = core::clamp(rot.Y, -90.f, 90.f);
					rot.Z = core::clamp(rot.Z, -90.f, 90.f);
					if (rot.getLength()>= 90)
						HasFinished = true;
					node->setRotation(rot);
					StartTime=timeMs;
				}
			}
		}

		void Rotation90BaseParent::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const
		{
			out->addVector3d("Rotation", Rotation);
		}

		void Rotation90BaseParent::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options)
		{
			Rotation = in->getAttributeAsVector3d("Rotation");
		}

		ISceneNodeAnimator* Rotation90BaseParent::createClone(ISceneNode* node, ISceneManager* newManager)
		{
			Rotation90BaseParent * newAnimator = 
				new Rotation90BaseParent(StartTime, Rotation);

			return newAnimator;
		}


	} // end namespace scene
} // end namespace irr

